/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tthdhm.truyvetduongdi.dao;

import com.tthdhm.truyvetduongdi.pojo.VictimInfoPOJO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import com.tthdhm.truyvetduongdi.util.Constants;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author BPT
 */
public class VictimInfoDAO {
    private static VictimInfoDAO instance;

    private VictimInfoDAO() {
    }

    public static VictimInfoDAO getInstance() {
        if (instance == null) {
            instance = new VictimInfoDAO();
        }
        return instance;
    }
    
    public List<VictimInfoPOJO> getListVictimInfoByUsername(String username){
        List<VictimInfoPOJO> victimInfoPOJOs=new ArrayList<VictimInfoPOJO>();
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        try {
            query = "Select * from TelephoneInfo, VictimInfo where TelephoneInfo.Username=? and TelephoneInfo.VictimID=VictimInfo.ID";
            conn = com.tthdhm.truyvetduongdi.util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setString(1, username);
            } else {
                return victimInfoPOJOs;
            }
            prepareStatement.execute();
            rs = prepareStatement.getResultSet();
            while(rs.next()) {
                VictimInfoPOJO victimInfoPOJO = new VictimInfoPOJO();
                // Set 
                victimInfoPOJO.setId(rs.getInt("ID"));
                victimInfoPOJO.setTelephoneID(rs.getString("TelephoneID"));
                victimInfoPOJO.setFullname(rs.getString("Fullname"));
                victimInfoPOJO.setSex(rs.getInt("Sex"));
                victimInfoPOJO.setBirthday(rs.getDate("Birthday"));
                victimInfoPOJO.setEmail(rs.getString("Email"));
                victimInfoPOJO.setRelation(rs.getString("Relations"));
                victimInfoPOJO.setTel(rs.getString("Tel"));
                victimInfoPOJOs.add(victimInfoPOJO);
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
        } finally {
            try {
                rs.close();
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
        return victimInfoPOJOs;
    }
    
    public VictimInfoPOJO getVictimInfoByID(String id){
        VictimInfoPOJO victimInfoPOJO=new VictimInfoPOJO();
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        try {
            query = "Select * from TelephoneInfo, VictimInfo where TelephoneInfo.TelephoneID=? and TelephoneInfo.VictimID=VictimInfo.ID";
            conn = com.tthdhm.truyvetduongdi.util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setString(1, id);
            } else {
                return victimInfoPOJO;
            }
            prepareStatement.execute();
            rs = prepareStatement.getResultSet();
            while(rs.next()) {
                victimInfoPOJO.setId(rs.getInt("ID"));
                victimInfoPOJO.setTelephoneID(rs.getString("TelephoneID"));
                victimInfoPOJO.setFullname(rs.getString("Fullname"));
                victimInfoPOJO.setSex(rs.getInt("Sex"));
                victimInfoPOJO.setBirthday(rs.getDate("Birthday"));
                victimInfoPOJO.setEmail(rs.getString("Email"));
                victimInfoPOJO.setRelation(rs.getString("Relations"));
                victimInfoPOJO.setTel(rs.getString("Tel"));
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
        } finally {
            try {
                rs.close();
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
        return victimInfoPOJO;
    }
    
}
